package dk.dashboardFP.panels.introspectionPanel {
	import dk.dashboardFP.elements.IElement;
	import dk.dashboardFP.panels.ui.text.DFPLabel;

	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author josef
	 */
	 
	 [Event(name="valueChange", type="dk.dashboardFP.panels.introspectionPanel.InputElementDFPEvent")]
	 
	public class InputElementDFP extends Sprite implements IElement {
		private var _label : DFPLabel;
		private var _labelText : String;
		private var _inputLabel : DFPLabel;
		private var _offsetContainer : Sprite;
		private var _delayTimer : Timer=new Timer(1, 1);
		private var _value : String;
		
		public function InputElementDFP(labelText : String = "") {
			_labelText = labelText;
			setupElements();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setupElements() : void {
			
			//:::::::
			// offset container 
			_offsetContainer = new Sprite();
			addChild(_offsetContainer);
			
			_offsetContainer.x = 5;
			_offsetContainer.y = 7;
			
			//:::::::
			// labe 
			if (_labelText != "") {
				_label = getLabel(_labelText),
				_offsetContainer.addChild(_label);
			}
			
			//:::::::
			// input 
			_inputLabel = new DFPLabel("", false);
			_offsetContainer.addChild(_inputLabel);
			
			if (_label) {
				_inputLabel.x = _label.x + _label.width + 5;
			}
			
			_inputLabel.makeInput(150, true);
			
			//:::::::
			// setup timer 
			_delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer, false, 0, true);
			
			//:::::::
			// register 
			_inputLabel.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
			
			mouseEnabled = false;
		}

		private function onKeyDown(event : KeyboardEvent) : void {
			delayedDispatch();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// value
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get value() : String {
			return _inputLabel.tfLabel.text;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// input handling
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function delayedDispatch() : void {
			_delayTimer.reset();
			_delayTimer.start();
		}

		private function onTimer(event : TimerEvent) : void {
			if (_value != _inputLabel.tfLabel.text) {
				dispatchEvent(new InputElementDFPEvent(InputElementDFPEvent.VALUE_CHANGE, true));
				_value = _inputLabel.tfLabel.text;
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// select
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function selectAll() : void {
			_inputLabel.tfLabel.setSelection(0, _inputLabel.tfLabel.text.length);
		}

		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// focus
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function setInFocus() : void {
			stage.focus=_inputLabel.tfLabel;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// config 
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		protected function getLabel(labelText : String) : DFPLabel {
			return new DFPLabel(labelText, false);
		}

		public function get nBottomLine() : Number {
			return 25;
		}
		
		public function set value(value : String) : void {
			_inputLabel.tfLabel.text=value;
			_value = value;
		}
	}
}
